package east2west.action;

import east2west.dto.CarDetailDTO;
import east2west.dto.CarModelDTO;
import east2west.dto.CarTypesDTO;
import east2west.dto.CarsDTO;


import east2west.service.CarDetailService;
import east2west.service.CarService;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author anhtuan
 */
public class CarAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    
    public ActionForward detail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int id = Integer.parseInt(request.getParameter("id"));
        //thong tin cua xe
        CarsDTO cardto = serCar.findById(id);
        //thong tin chi tiet xe
        CarDetailService serCarDetail = new CarDetailService();
        ArrayList<CarDetailDTO> listdetail = serCarDetail.findByCarId(id);
        //lay thong tin tat ca cac xe khac id
        ArrayList<CarsDTO> listOther = serCar.readByOtherCarId(id);


        request.setAttribute("page", "cardetail.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listdetail", listdetail);
        request.setAttribute("listcar", cardto);
        request.setAttribute("listOther", listOther);
        return mapping.findForward(SUCCESS);
    }

   
    public ActionForward listcar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        ArrayList<CarsDTO> listAll = serCar.readByAll();
        request.setAttribute("page", "carlist.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listall", listAll);
        return mapping.findForward(SUCCESS);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element :
     * ( <action parameter="method" .../> )
     */
    public ActionForward searchmodel(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int id = Integer.parseInt(request.getParameter("id"));
        ArrayList<CarsDTO> listSearch = serCar.findByModeId(id);
        request.setAttribute("page", "carsearch.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listsearch", listSearch);
        return mapping.findForward(SUCCESS);

    }

    public ActionForward searchtype(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int id = Integer.parseInt(request.getParameter("id"));
        ArrayList<CarsDTO> listSearch = serCar.findByTypeId(id);
        request.setAttribute("page", "carsearch.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listsearch", listSearch);
        return mapping.findForward(SUCCESS);

    }

    public ActionForward searchaircondition(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        byte air = new Byte(request.getParameter("air"));
        ArrayList<CarsDTO> listAir = serCar.findByAirConditional(air);
        request.setAttribute("page", "carsearch.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listsearch", listAir);
        return mapping.findForward(SUCCESS);

    }

    public ActionForward searchseat(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        int qty = Integer.parseInt(request.getParameter("qty"));
        ArrayList<CarsDTO> listSeat = serCar.findBySeatQty(qty);
        request.setAttribute("page", "carsearch.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listsearch", listSeat);
        return mapping.findForward(SUCCESS);

    }

    public ActionForward searchCar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CarService serCar = new CarService();
        CarsDTO dto = new CarsDTO();


        String carName = request.getParameter("carname");

        dto.setCarName(carName);

        CarTypesDTO typeDTO = new CarTypesDTO();
        typeDTO.setTypeName(request.getParameter("type"));
        dto.setTypeDTO(typeDTO);

        CarModelDTO modelDTO = new CarModelDTO();
        modelDTO.setModelName(request.getParameter("model"));
        dto.setModelDTO(modelDTO);

        try {
            int seatMin = Integer.parseInt(request.getParameter("seatMin"));
            dto.setSeatQty(seatMin);
        } catch (Exception e) {
        }
        try {
            int seatMax = Integer.parseInt(request.getParameter("seatMax"));
            dto.setSeatQtyMax(seatMax);
        } catch (Exception e) {
        }
        try {
            byte air = new Byte(request.getParameter("air"));
            dto.setAirConditional(air);
        } catch (Exception e1) {
        }
        ArrayList<CarsDTO> listsearch = serCar.findByDTO(dto);
        request.setAttribute("page", "carsearch.page");
        request.setAttribute("pages", "Cars");
        request.setAttribute("listsearch", listsearch);
        //request.setAttribute("page", "cartest.page");
        //request.setAttribute("listsearch", dto);
        return mapping.findForward(SUCCESS);

    }
}
